package com.baturu.vin.benz.cleanData.dao;

import com.baturu.vin.benz.dto.BenzDealPartsDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 处理奔驰bm_parts2_v数据合并DAO
 * 用于数据清洗
 * @author jiangxinlei
 * @Time 2017/3/6.
 */
public interface BenzDealPartsDAO {

    @Select({" select catnum,groupnum,subgrp from catnum_groupnum_subgrp"})
    List<BenzDealPartsDTO> getcatnumgroupsub();

    @Insert({"<script> insert into bm_parts2_v_newdata (catnum,groupnum,subgrp,seqnum,seqno,callout,replflg,parttype,partnum,nounidx,",
            "descidx,indent,tuvsign,optflag,changeflag,steering,type,reptype,reppno,footnotes,",
            "neutral,codeb,quantbm,components,submods,reppart,optpart,damage_part,ranking_number,repprem,partrem,pos_addr,vpd_ident,cbPartsCode) values " ,
            "<foreach item='bmPart' index='index' collection='bmParts' separator=','> ",
            "(#{bmPart.catnum},#{bmPart.groupnum},#{bmPart.subgrp},#{bmPart.seqnum},#{bmPart.seqno},#{bmPart.callout},#{bmPart.replflg},#{bmPart.parttype},#{bmPart.partnum},#{bmPart.nounidx},",
            "#{bmPart.descidx},#{bmPart.indent},#{bmPart.tuvsign},#{bmPart.optflag},#{bmPart.changeflag},#{bmPart.steering},#{bmPart.type},#{bmPart.reptype},#{bmPart.reppno},#{bmPart.footnotes},",
            "#{bmPart.neutral},#{bmPart.codeb},#{bmPart.quantbm},#{bmPart.components},#{bmPart.submods},#{bmPart.reppart},#{bmPart.optpart},#{bmPart.damage_part},#{bmPart.ranking_number},#{bmPart.repprem},",
            "#{bmPart.partrem},#{bmPart.pos_addr},#{bmPart.vpd_ident},#{bmPart.cbPartsCode})",
            "</foreach>",
            "</script>"})
    int insertBMParts(@Param("bmParts") List<BenzDealPartsDTO> bmparts);

    @Select({"select descidx, id from bm_parts2_v_newdata where id >= #{id} and id <= #{endId}"})
    List<BenzDealPartsDTO> getPartsDesc(@Param("id") Integer id, @Param("endId") Integer endId);


    @Select({"select distinct descidx from part_descs_v_newdata where description is not null"})
    List<String> getAllDescs();

    @Insert({"<script>insert into bm_parts2_v_newdata(descidx, id) values ",
            "<foreach item='dto' index='index' collection='dtos' open='' separator=',' close=''>",
            "(#{dto.descidx},#{dto.id})",
            "</foreach>",
            "ON DUPLICATE KEY UPDATE",
            "descidx=values(descidx)",
            "</script>"})
    int updateDesc(@Param("dtos") List<BenzDealPartsDTO> dtos);

    @Insert({"insert into ${tableName}${insertName} values ${insertValue}"})
    int insert(@Param("tableName") String tableName,
               @Param("insertName") String insertName,
               @Param("insertValue") String insertValue);

    @Select("select max(id) from bm_parts2_v_newdata")
    int queryBmPartsMaxId();
}
